const gulp = require('gulp');

const runSequence = require('run-sequence');
const gulpDebug = require('gulp-debug');
const gulpChanged = require('gulp-changed');
const htmlBeautify = require('gulp-html-beautify');

const del = require('del');

// tools
const devConf = require('./build/dev-config');
const conf = require('./build/conf');
const utils = require('./build/utils');

const globs = conf.globs;
const browserSync = utils.browserSync;
const debug = utils.debug();

// base变量 路径变量
const EVN_DEV = conf.EVN_DEV;
const EVN_PRO = conf.EVN_PRO;

/* clean EVN_PRO file */
gulp.task('clean', function(cb) {
	debug.info('delete history file');
	return del(`${EVN_PRO}/`, cb);
});

/* clean html */
gulp.task('clean-html', function(cb) {
	debug.info('delete history html file');
	return del(`${EVN_PRO}/**/*.html`, cb);
});

/* treat assets */
gulp.task('assets', function() {
	return gulp
		.src(globs.assets)
		.pipe(gulpChanged(EVN_PRO))
		.pipe(
			gulpDebug({
				title: '处理引用资源'
			})
		)
		.pipe(gulp.dest(EVN_PRO))
		.pipe(browserSync.stream());
});

/* include html file  */
gulp.task('include', function(cb) {
	return gulp.src(globs.include).pipe(utils.includeToAbsolutePath());
	// .pipe(browserSync.stream());
});

/* html file  */
gulp.task('html', function(cb) {
	return gulp
		.src(globs.html)
		.pipe(gulpChanged(EVN_PRO))
		.pipe(utils.exchangeInclude())
		.pipe(
			htmlBeautify({
				indent_size: 2,
				max_preserve_newlines: 0
			})
		)
		.pipe(gulp.dest(EVN_PRO))
		.pipe(browserSync.stream());
});

// 开发队列
gulp.task('dev-queue', function(cb) {
	runSequence('clean', 'include', ['html', 'assets'], cb);
});

// include 队列
gulp.task('include-queue', function(cb) {
	runSequence('clean-html', 'include', 'html', cb);
});

/* 开发 */
gulp.task('dev', ['dev-queue'], function() {
	utils.initServerProxy(devConf.dev);
	gulp.watch(globs.assets, ['assets']);
	gulp.watch(globs.html, ['html']);
	gulp.watch(globs.include, ['include-queue']);
});

/* 生产模式打包 */
gulp.task('prod', ['dev-queue']);
gulp.task('ie8', ['prod'], function () {
	gulp.watch(globs.assets, ['assets']);
	gulp.watch(globs.html, ['html']);
	gulp.watch(globs.include, ['include-queue']);
	gulp.watch(globs.scss, ['scss']);
	require('./build/server');
});
